package com.ipay.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ipay.entity.CashiersEntity;
import com.ipay.entity.QRCodeEntity;
import com.ipay.service.CashiersService;
import com.ipay.service.QRCodeService;
import com.ipay.utils.Constant;
import com.ipay.utils.PageUtils;
import com.ipay.utils.Query;
import com.ipay.utils.R;

/**
 * 支付渠道商管理
 * 
 * @author Luohx
 */
@Transactional
@Controller
@RequestMapping("/qrCode")
public class QrCodeController extends AbstractController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private QRCodeService qRCodeService;
	@Autowired
	private CashiersService cashiersService;

	@RequestMapping("/{url}.html")
	public String page(@PathVariable("url") String url) {
		return "qrCode/" + url + ".html";
	}
	
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ResponseBody
	@RequiresPermissions("qrCode:list")
	public R list(@RequestParam Map<String, Object> params) {
		logger.info("params==" + params.toString());
		// 查询列表数据
		params.put("deleteStatus", Constant.DELETE_STATUS_NO);
		Query query = new Query(params);
		logger.info("query==" + query.toString());

		List<QRCodeEntity> qrCodeList = qRCodeService.queryList(query);
		int total = qRCodeService.queryTotal(query);
		logger.info("total==" + total);

		PageUtils pageUtil = new PageUtils(qrCodeList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 信息
	 * @throws IOException 
	 */
	@RequestMapping("/info/{id}")
	@ResponseBody
	@RequiresPermissions("cashiers:info")
	public R info(@PathVariable("id") Long id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		CashiersEntity cashiers = cashiersService.queryObject(id);
		return R.ok().put("qrCode", cashiers);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@ResponseBody
	@RequiresPermissions("qrCode:save")
	public R save(@RequestBody QRCodeEntity qrCodeEntity) {
		//根据收款员id 银行id 查询是否已经添加二维码信息
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", qrCodeEntity.getUserId());
		map.put("bankId", qrCodeEntity.getBankId());
		
		List<QRCodeEntity> codeList = qRCodeService.queryList(map);
		if(codeList.size() > 0){
			return R.error("当前用户在该银行下已添加二维码，无需重复添加");
		}
		qRCodeService.save(qrCodeEntity);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	@RequiresPermissions("qrCode:delete")
	public R delete(@RequestBody String[] ids) {
		qRCodeService.deleteBatch(ids);
		return R.ok();
	}
	
	/**
	 * 批量添加二维码
	 */
	@RequestMapping("/addBatchCode")
	@ResponseBody
	@RequiresPermissions("qrCode:addBatchCode")
	public R addBatchCode(@RequestBody QRCodeEntity qrCode) {
		//根据银行卡id、商户号查询该商户下还未添加二维码的 收款员集合
		boolean flag = qRCodeService.addBatchCode(qrCode);
		if(flag){
			return R.ok();
		}else{
			return R.error();
		}
	}
}
